define(['jquery', 'durandal/app', 'durandal/composition', 'knockout', 'http', 'view/builder/human/i18nHuman', 'tree', 'bootbox',
    "css!style/builder/human/page-style"
], function ($, app, composition, ko, http, humanI18n, tree, box) {
    var getHumanInfoByHumanId = http.getInstance("home/builder/getHumanInfoByHumanId", {type: "POST"});
    var updateHumanNavBar = http.getInstance("home/builder/updateHumanNavBar", {type: "POST"});
    var updateHumanInfo = http.getInstance("home/builder/updateHumanInfo", {type: "POST"});
    var addHuman = http.getInstance("home/builder/addHuman", {type: "POST"});
    var Human = new function () {
        var that = this;
        that.selectHumanId = -1;
        that.init = function () {
            composition.addBindingHandler('application-builder-human', {
                init: function (dom) {
                    that._dom = dom;
                    that.renderUI();
                    that.bindUI();
                }
            });
        };
        //所有的点击事件
        that.clickEvent = {
            eventSaveNav: function (e) {
                if (that.selectHumanId) {
                    var thisTree = $('#human-right-tree');
                    var updateNavIdList = [];
                    $.each(thisTree.tree('getRoots'), function (index, node) {
                        var obj = thisTree.tree('getData', node.target);
                        var children = obj.children;
                        $.each(children, function (_index, child) {
                            var childUpdateFlag = false;
                            var leafs = child.children;
                            if(leafs.length === 0 && child.checked){
                                childUpdateFlag = true;
                            }
                            $.each(leafs, function (__index, leaf) {
                                if (leaf.checked) {
                                    childUpdateFlag = true;
                                    updateNavIdList.push(leaf.id);
                                }
                            });
                            if (childUpdateFlag) {
                                updateNavIdList.push(child.id);
                            }
                        });
                    });
                    updateHumanNavBar.ajax({
                        humanId: that.selectHumanId,
                        updateList: updateNavIdList
                    }).then(function (data) {
                        box.alert({
                            size: "small",
                            title: "保存成功!",
                            message: '保存成功'
                        });
                    });
                } else {
                    box.alert({
                        size: "small",
                        title: "保存失败!",
                        message: '请先在左侧人员树中选择人员'
                    });
                    return true;
                }
            },
            eventModifyHumanInfo: function (e) {
                if(!that.validateHumanName()){
                    box.alert({
                        size:"small",
                        title:"错误",
                        message:"请填写人员姓名"
                    });
                    return;
                }

                if(!that.selectHumanId){
                    box.alert({
                        size: "small",
                        title: "错误!",
                        message: '请先在左侧人员树中选择人员'
                    });
                    return falsel;
                }

                var updateInfo = {
                    humanName: vm.humanName(),
                    humanEmail: vm.humanEmail(),
                    humanPhoneNum: vm.humanPhoneNum(),
                    humanPassword: vm.humanPassword(),
                    humanId:that.selectHumanId,
                    dutyId:that.dutyId
                };
                updateHumanInfo.ajax(updateInfo).then(function(){
                    that.renderUI();
                    box.alert({
                        size: "small",
                        title: "修改成功!",
                        message: '修改成功!'
                    });
                }).error(function(data){
                    box.alert({
                        size: "small",
                        title: "保存失败!",
                        message: data.msg
                    });
                });

            },
            eventAddHuman: function (e) {
                if(!that.validateHumanName()){
                    box.alert({
                        size:"small",
                        title:"请填写人员姓名",
                        message:"请填写人员姓名"
                    })
                }
                var insertInfo = {
                    humanName: vm.humanName(),
                    humanEmail: vm.humanEmail(),
                    humanPhoneNum: vm.humanPhoneNum(),
                    humanPassword: vm.humanPassword()
                };
                addHuman.ajax(insertInfo).then(function(){
                    that.renderUI();
                }).error(function(data){
                    box.alert({
                        size: "small",
                        title: "添加失败!",
                        message: data.msg
                    });
                });
            }

        };

        that.validateHumanName = function () {
            var name =    vm.humanName();
            return name.length !== 0
        };

        that.bindUI = function () {
            vm.eventSaveNav = function (e) {
                $('#save-right-btn').button('loading');
                setTimeout(that.clickEvent.eventSaveNav(e), 100);
                $('#save-right-btn').button('reset');
            };

            vm.eventModifyHumanInfo = function(e){
                $("#modify-human-info-btn").button('loading');
                setTimeout(that.clickEvent.eventModifyHumanInfo(e),100);
                $("#modify-human-info-btn").button('reset');
            };
            
            vm.eventAddHuman = function (e) {
                $('#add-human-btn').button('loading');
                setTimeout(that.clickEvent.eventAddHuman(e),100);
                $('#add-human-btn').button('reset');
            };
        };
        that.renderUI = function () {
            that.humanTree = $('#human-tree').tree({
                url: cywork.global.rootPath + "home/builder/getHumanList",
                checkbox: false,
                cascadeCheck: true,
                onClick: function (node) {
                },
                onDblClick: function (node) {
                    var id = node.id;
                    if (id < 0) {
                    } else {
                        that.selectHumanId = id;
                        getHumanInfoByHumanId.ajax({humanId: id}).then(that.putHumanInfo);
                    }
                }
            });
        };

        that.putHumanInfo = function (data) {
            if (data.human) {
                var human = data.human;
                vm.humanCode(human.humanCode);
                vm.humanName(human.humanName);
                vm.humanId(human.humanId);
                vm.humanEmail(human.email);
                vm.humanPassword("");
                vm.humanPhoneNum(human.phoneNumber);
                that.humanRightTree = $('#human-right-tree').tree({
                    url: cywork.global.rootPath + "home/builder/getHumanRightTree?humanId=" + human.humanId + "&t=" + (new Date()).getTime(),
                    checkbox: true,
                    cascadeCheck: true,
                    onClick: function (node) {
                        return;//会向后台发送许多的请求,暂时不处理
                        var id = node.id;
                    },
                    onDblClick: function (node) {
                        var id = node.id;
                    }
                });
            }
        };
    };

    Human.init();

    var vm = {
        textHumanName: ko.observable(humanI18n.textHumanName),
        textHumanId: ko.observable(humanI18n.textHumanId),
        textHumanCode: ko.observable(humanI18n.textHumanCode),
        textHumanPassword: ko.observable(humanI18n.textHumanPassword),
        textHumanPhoneNum: ko.observable(humanI18n.textHumanPhoneNum),
        textHumanEmail: ko.observable(humanI18n.textHumanEmail),
        textDutyName: ko.observable(humanI18n.textDutyName),
        textModify: ko.observable('修改'),
        textAdd: ko.observable('新增人员'),
        humanName: ko.observable(""),
        humanId: ko.observable(),
        humanCode: ko.observable(""),
        humanPassword: ko.observable(""),
        humanPhoneNum: ko.observable(""),
        humanEmail: ko.observable(""),
        eventModifyHumanInfo: ko.observable(),
        eventAddHuman: ko.observable(),
        eventSaveNav: ko.observable(),
        dutyId: ko.observable(),
    };
    return vm;
});